<?php
namespace app\api\controller;

use \think\Controller;
use \think\Db;
class My extends Common
{
    /**
     *我的——点击进入的首页显示
     * user_id
     * @return string
     */
    public function index(){
        $uid = $_POST['user_id'];
        $data = array();
        $info = Db::table('up_u_user')->where('user_id',$uid)
            ->field('user_id,user_username,user_image,user_integral,user_attestation')->find();
            $data['myInfo'] = $info;
        $data['liulang'] = Db::table('up_dog')
            ->where('user_id',$uid)//我关注的
            ->where('dog_type',1)
//            ->where('dog_preserve','2')//不是暂存的
            ->count();
        $data['huzhu'] = Db::table('up_dog')
            ->where('user_id',$uid)//我关注的
            ->where('dog_type',2)
            ->count();
        $data['lingyang'] = Db::table('up_dog')
            ->where('user_id',$uid)//我关注的
            ->where('dog_type',3)
            ->count();
        $data['xungou'] = Db::table('up_dog')
            ->where('user_id',$uid)//我关注的
            ->where('dog_type',4)
            ->count();
        return json_encode(['status'=>200,'msg'=>$data]);
    }
    /**
     *我的——我的关注
     * user_id，type(1是用户，2是狗),page
     * @return string
     */
    public function myGuanzhu(){
        $uid = $_POST['user_id'];
        $type = $_POST['type'];
        $page = $_POST['page'];
        $data = array();
        $users = Db::table('up_u_user_interest_user')->alias('uu')
            ->join('up_u_user u','uu.uu_attention_userid=u.user_id','LEFT')
            ->where('uu_userid',$uid)//我关注的用户
            ->field('u.user_id,u.user_username,u.user_image,u.user_attestation,u.user_integral')
            ->limit(($page-1)*10,10)
            ->select();
        $data['user'] = count($users);
        $dogs = Db::table('up_u_user_interest_dog')->alias('d')
            ->join('up_dog_family f','d.udf_id=f.udf_id','LEFT')
            ->where('d.user_id',$uid)//我关注的用户
            ->field('f.udf_name,f.udf_id,f.udf_img,f.udf_sex,f.udf_age')
            ->limit(($page-1)*10,10)
            ->select();
        $data['dog'] = count($dogs);
        if($type == 1){
            $data['data'] = $users;
        }
        if($type == 2){
            $data['data'] = $dogs;
        }
        return json_encode(['status'=>200,'msg'=>$data]);
    }

    /**
     * user_id,page
     * 我的流浪狗
     */
    public function myStrayDog()
    {
        $uid = $_POST['user_id'];
        $page = $_POST['page'];
        $datas = Db::table('up_dog')->alias('d')
            ->where('d.user_id',$uid)
            ->where('d.dog_type',1)
            ->where('dog_preserve','2')//不是暂存的
            ->order('dog_release_time DESC')
            ->field('dog_id,dog_num,udsc_id,udsc_id_information,dog_information,dog_status,dog_img')
            ->limit(($page-1)*10,10)->select();
        if($datas){
            foreach ($datas as $k=>$v){
                $tz = trim($v['udsc_id'],',');
                if($tz){
                    $tzs = explode(',',$tz);
                    $datas[$k]['styles'] = Db::table('up_dog_strat_characteristic')
                        ->where('udsc_id','in',$tzs)->column('udsc_name');
                    if(in_array(0,$tzs)){
                        $datas[$k]['styles'][] = $v['udsc_id_information'];
                    }
                }
            }
        }
        return json_encode(['status'=>200,'msg'=>$datas]);
    }
    /**
     * dog_id,status，type
     * 修改我的各种狗的状态
     */
    public function updateDogStatus()
    {
        $did = $_POST['dog_id'];
        $status = $_POST['status'];
        $type = $_POST['type'];
        if($type == 1){$dt = 'dog_status';}if($type == 2){$dt = 'dog_help_status';}
        if($type == 3){$dt = 'dog_adopt_status';}if($type == 4){$dt = 'dog_seek_status';}
        // 启动事务
        Db::startTrans();
        try{
        $res = Db::table('up_dog')->where('dog_id',$did)->update([$dt=>$status,'dog_update_time'=>date('Y-m-d H:i:s')]);
        $again = Db::table('up_dog')->where('dog_id',$did)->find();//自动发布一个动态
        //领养的状态更改一下
        if($type == 3){
            if($status == 1){$status2=3;}else{$status2=4;}
            Db::table('up_dog_proposer')->where('dog_id',$did)->update(['proposer_status'=>$status2]);
            if($status == 4){
                $data = array();
                $data['dog_adopt_reason'] = $_POST['reason'];
                $data['dog_adopt_explain'] = $_POST['explain'];
                $data['dog_adopt_qita_explain'] = $_POST['qita_explain'];
                if($_POST['reason']){
                    $ling = trim($_POST['reason'],',');
                    $ling = explode(',',$ling);
                    if(in_array(0,$ling)){
                        $data['dog_adopt_other_reason'] = $_POST['other_reason'];
                    }
                }
                Db::table('up_dog')->where('dog_id',$did)->update($data);
                //=============自动插入一条动态=============
                $again['dog_release_time'] = $again['dog_update_time'];
                $again['dog_apply_num'] = 0;
                $again['dog_adopt_status'] = 2;
                $again['dog_preserve'] = 2;
                $again['dog_attention_num'] = 2;
                $again['dog_time'] = date('Y-m-d');
                unset($again['dog_id']);
                $newdid = Db::name('dog')->insertGetId($again);
                //===================end=================
                //=============自动插入两条默认领养人=============
                $pro = array();
                $pro['proposer_user_id'] = -1;
                $pro['dog_id'] = $newdid;
                $pro['proposer_status'] = 1;
                Db::table('up_dog_proposer')->insert($pro);
                $pro2 = array();
                $pro2['proposer_user_id'] = -2;
                $pro2['dog_id'] = $newdid;
                $pro2['proposer_status'] = 1;
                Db::table('up_dog_proposer')->insert($pro2);
                //===================end======================
            }

        }
            // 提交事务
            Db::commit();
        return json_encode(['status'=>200,'msg'=>'更改成功']);
        //=====end===============
        } catch (\Exception $e) {
            return json_encode(['status'=>0,'msg'=>'更改失败或者没有修改']);
        }
    }
    /**
     * dog_id
     * 删除我的流浪狗（各种狗）
     */
    public function deleteDog()
    {
        $did = $_POST['dog_id'];
        //如果是领养的话就把所有的领养这个狗的申请都删除掉
        $type = Db::table('up_dog')->where('dog_id',$did)->value('dog_type');
        if($type == 3){
           Db::table('up_dog_proposer')->where('dog_id',$did)->delete();
        }
        //===========*end*=============
        $res = Db::table('up_dog')->where('dog_id',$did)->delete();
        if($res){
            return json_encode(['status'=>200,'msg'=>'删除成功']);
        }else{
            return json_encode(['status'=>0,'msg'=>'删除失败']);
        }
    }
    /**
     * dog_id
     * 发布我的流浪狗的进展
     */
    public function insertStrayDogJinzhan()
    {
        $data = array();
        $data['j_dog_id'] = $_POST['dog_id'];
        $data['j_content'] = $_POST['content'];
        if($_POST['image']){
            $data['j_img'] = trim($_POST['image'],',');
        }
        $data['j_zan'] = 0;
        $data['j_datetime'] = date('Y-m-d H:i:s');
        $res = Db::table('up_jinzhan')->insert($data);
        if($res){
            return json_encode(['status'=>200,'msg'=>'发布成功']);
        }else{
            return json_encode(['status'=>0,'msg'=>'发布失败']);
        }
    }

    //==============================我的互助==============================

    /**
     * user_id，page，type（1暂存，2发布）
     * 我发布的互助狗狗
     */
    public function myHelpDog()
    {
        $uid = $_POST['user_id'];
        $page = $_POST['page'];
        $type = $_POST['type'];
        $datas = Db::table('up_dog')->alias('d')
            ->join('up_dog_family f','d.udf_id=f.udf_id','LEFT')
            ->join('up_dog_heo h','d.dog_help_style=h.heo_id','LEFT')
            ->where('d.user_id',$uid)
            ->where('d.dog_type',2)
            ->where('dog_preserve',$type)//不是暂存的
            ->order('dog_release_time DESC')
            ->field('dog_id,udf_name,dog_information,dog_help_style,dog_help_status,dog_help_information,dog_end_time,dog_img')
            ->limit(($page-1)*10,10)->select();
        if($datas){
            foreach ($datas as $k=>$v){
                $tz = trim($v['dog_help_style'],',');
                if($tz){
                    $tzs = explode(',',$tz);
                    $datas[$k]['styles'] = Db::table('up_dog_heo')
                        ->where('heo_id','in',$tzs)->column('heo_name');
                    if(in_array(0,$tzs)){
                        $datas[$k]['styles'][] = $v['dog_help_information'];
                    }
                }
            }
        }
        return json_encode(['status'=>200,'msg'=>$datas]);
    }
    /**
     * user_id，dog_id
     * 获取我暂存的互助狗狗的详情
     */
    public function getMyHelpDoginfo()
    {
        $uid = $_POST['user_id'];
        $did = $_POST['dog_id'];
        $data = Db::table('up_dog')->alias('d')
            ->join('up_dog_family f','d.udf_id=f.udf_id','LEFT')
            ->join('up_dog_heo h','d.dog_help_style=h.heo_id','LEFT')
            ->where('d.dog_id',$did)
            ->field('dog_information,dog_id,udf_name,d.udf_id,dog_help_style,dog_help_status,dog_help_information,dog_end_time,dog_img')
            ->find();
        $data['dog_end_time'] = substr($data['dog_end_time'],0,-3);
        $s = trim($data['dog_help_style'],',');
        $img = trim($data['dog_img'],',');
        $data['dog_help_style_name'] = '';
        $datastyle = array();
        if($s){
            $data['dog_help_style'] = explode(',',$s);
            $name = Db::table('up_dog_heo')
                ->where('heo_id','IN',$data['dog_help_style'])
                ->column('heo_name');
            if($name){
                $datastyle = $name;
            }
            if(in_array(0,$data['dog_help_style'])){
                $datastyle[] = '其他';
            }
            if($datastyle){
                $data['dog_help_style_name'] = implode($datastyle,',');
            }

        }else{
            $data['dog_help_style'] = array();
        }
        if($img){
            $data['dog_img'] = explode(',',$img);
        }else{
            $data['dog_img'] = array();
        }
        $types = Db::table('up_dog_heo')->select();
        $dogs = Db::table('up_dog_family')->where('user_id',$uid)->select();
        return json_encode(['status'=>200,'msg'=>$data,'types'=>$types,'dogs'=>$dogs]);
    }
    /**
     * user_id，udf_id
     * 发布或者再次暂存之前未发布的互助狗狗
     */
    public function fabuMyHelpDog()
    {
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }

        if($dog_img == ""){
            if($type == 1){
                $img = Db::table('up_dog_family')
                    ->where('udf_id',$udf_id)
                    ->value('udf_img');
                if($img){
                    $dog_img = trim($img,',');
                }
            }
        }
        /*前台收集到的表单数据*/
        $data = [
            'dog_type'=>2,
            'user_id'=>$user_id,
            'udf_id'=>$udf_id,
            'area_sheng'=>$sheng,
            'area_shi'=>$shi,
            'area_qu'=>$qu,
            'dog_help_style'=>$help_style,
            'dog_end_time'=>$end_time,
            'dog_img'=>$dog_img,
            'dog_lon'=>$lon,
            'dog_lat'=>$lat,
            'dog_help_information' => $help_information,
            'dog_help_status'=>1,
            'dog_information'=>$information
        ];
        $time = date('Y-m-d H:i:s');
        if($type == 1){
            $data['dog_preserve'] = 2;
            $data['dog_release_time'] = $time;
            $data['dog_time'] = date('Y-m-d');
            $data['dog_update_time'] = $time;
            // 启动事务
            Db::startTrans();
            try{
                Db::table('up_dog')
                    ->where('dog_id',$dog_id)->update($data);
                $res = Db::table('up_integral')
                    ->where(['integral_status'=>3,'integral_user_id'=>$user_id])
                    ->whereTime('integral_time', 'today')
                    ->count();
                if($res<2){
                    Db::table('up_integral')
                        ->insert(['integral_status'=>3,'integral_user_id'=>$user_id,'integral_time'=>date('Y-m-d H:i:s'),'integral_num'=>5,'integral_dog_id'=>$dog_id]);
                    Db::table('up_u_user')
                        ->where('user_id',$user_id)
                        ->setInc('user_integral',5);
                }
                // 提交事务
                Db::commit();
                $returnData['status'] = 200;
                $returnData['msg'] = "成功！";
                return json_encode($returnData);
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                $returnData['status'] = 0;
                $returnData['msg'] = "失败！";
                return json_encode($returnData);
            }

        }else if($type == 2){
            $data['dog_preserve'] = 1;
            $data['dog_update_time'] = $time;
            $res = Db::table('up_dog')
                ->where('dog_id',$dog_id)->update($data);
            if($res){
                $returnData['status'] = 200;
                $returnData['msg'] = "成功！";
                return json_encode($returnData);
            }else{
                $returnData['status'] = 0;
                $returnData['msg'] = "失败！";
                return json_encode($returnData);
            }
        }

    }


    /**
     * user_id，page,type
     * 我的领养的狗
     */
    public function myAdoptDog()
    {
        $uid = $_POST['user_id'];
        $page = $_POST['page'];
        $type = $_POST['type'];
        $datas = Db::table('up_dog')->alias('d')
            ->join('up_dog_family f','d.udf_id=f.udf_id','LEFT')
            ->where('d.user_id',$uid)
            ->where('d.dog_type',3)
            ->where('dog_preserve',$type)//是不是暂存的，1暂存2发布
            ->order('dog_release_time DESC')
            ->field('dog_id,udf_name,udf_sex,udf_age,udf_dog_vaccine,udf_dog_outside,udf_dog_inside,udf_dog_sterilization,udf_dog_disability,dog_adopt_status,dog_img')
            ->limit(($page-1)*10,10)->select();
        return json_encode(['status'=>200,'msg'=>$datas]);
    }
    /**
     * user_id，page,type
     * 此条领养的申请记录
     */
    public function myAdoptDogJilu()
    {
//        $uid = $_POST['user_id'];
        $did = $_POST['dog_id'];
        $page = $_POST['page'];
        $datas = Db::table('up_dog_proposer')->alias('d')
            ->join('up_u_user u','d.proposer_user_id=u.user_id','LEFT')
            ->join('up_area a','d.proposer_abode_city=a.district_id','LEFT')
            ->where('d.dog_id',$did)
            ->where('d.proposer_me_cancle',1)
            ->order('proposer_datetime DESC')
            ->field('proposer_status,d.proposer_user_id,dog_id,proposer_id,district,user_id,user_rel_name,user_username,user_gender,user_image,user_integral,user_attestation,proposer_datetime')
            ->limit(($page-1)*10,10)->select();
        $can = 2;//不可再选择
        if($datas){
            if($datas[0]['proposer_status'] == 1){
                $can = 1;//可选择
            }
        }

        return json_encode(['status'=>200,'msg'=>$datas,'can'=>$can]);
    }
    /**
     * proposer_id
     * 选定领养人
     */
    public function myAdoptDogChoose()
    {
        $pid = $_POST['proposer_id'];
        //=============
        $have = Db::table('up_dog_proposer')->where('proposer_id',$pid)->find();
        $have2 = Db::table('up_dog_proposer')
            ->where('dog_id',$have['dog_id'])
            ->where('proposer_status',2)
            ->find();
        if($have2){
            return json_encode(['status'=>0,'msg'=>'已经有人领养了']);
        }
        // 启动事务
        Db::startTrans();
        try{
        //==========================
        //先把其他人的状态改为失败，再把选中的改为选中
        Db::table('up_dog_proposer')->where('dog_id',$have['dog_id'])
            ->update(['proposer_status'=>0,'proposer_suretime'=>date('Y-m-d H:i:s')]);
        $res = Db::table('up_dog_proposer')->where('proposer_id',$pid)
            ->update(['proposer_status'=>2,'proposer_suretime'=>date('Y-m-d H:i:s')]);

        //把dog表里的这个动态的领养状态改成3
        $data = array();
        $data['dog_adopt_status'] = 3;
        $data['dog_adopt_new_userid'] = $have['proposer_user_id'];
        $data['dog_adopt_number'] = date('YmdHis') . rand(1000,9999);
        Db::table('up_dog')->where('dog_id',$have['dog_id'])
            ->update($data);
            //领养完成积分+10
            if($have['proposer_user_id'] > 0) {
                Db::table('up_integral')
                    ->insert(['integral_status' => 5, 'integral_user_id' => $have['proposer_user_id'], 'integral_time' => date('Y-m-d H:i:s'), 'integral_num' => 10]);
                Db::table('up_u_user')
                    ->where('user_id', $have['proposer_user_id'])
                    ->setInc('user_integral', 10);
                //发送短信
                $phone = Db::table('up_u_user')
                    ->where('user_id', $have['proposer_user_id'])
                    ->find()['user_phone'];
                $num = 1;
                $this->duaxin($phone,$num);
            }
            // 提交事务
            Db::commit();
            return json_encode(['status'=>200,'msg'=>'选择成功']);
        } catch (\Exception $e) {
            return json_encode(['status'=>0,'msg'=>'选择失败']);
        }
    }
    /**
     * proposer_id
     * 查看领养人详情
     */
    public function askAdoptDogUserInfo()
    {
        $pid = $_POST['proposer_id'];
        $data = Db::table('up_dog_proposer')->alias('d')
            ->join('up_u_user u','d.proposer_user_id=u.user_id','LEFT')
            ->join('up_area a','d.proposer_abode_province=a.district_id','LEFT')
            ->join('up_area a2','d.proposer_abode_city=a2.district_id','LEFT')
            ->join('up_area a3','d.proposer_abode_district=a3.district_id','LEFT')
            ->join('up_proposer_environment hj','d.proposer_environment=hj.environment_id','LEFT')
            ->join('up_proposer_house h','d.proposer_house=h.house_id','LEFT')
            ->where('d.proposer_id',$pid)
            ->field('d.*,proposer_environment_other,proposer_house_other,hj.environment_name,h.house_name,user_card,a.district as province,a2.district as city,a3.district as district,user_id,user_username,user_gender,user_attestation,user_rel_name')
            ->find();
        if($data['proposer_environment_other']){
            $data['environment_name'] = $data['proposer_environment_other'];
        }
        if($data['proposer_house_other']){
            $data['house_name'] = $data['proposer_house_other'];
        }
        if($data['proposer_petyet'] == 1){
            $data['proposer_petyet'] = '有';
        }else{
            $data['proposer_petyet'] = '无';
        }

        if($data['user_card'] != ""){
            $user_year = substr($data['user_card'],6,4);
            $year = date('Y');
            $data['user_card'] = $year-$user_year;
        }
        $data['proposer_cohabit'] = trim($data['proposer_cohabit'],',');
        $data['proposer_reason'] = trim($data['proposer_reason'],',');
        $data['proposer_care'] = trim($data['proposer_care'],',');

        $data['proposer_cohabit'] = explode(',',$data['proposer_cohabit']);
        $data['proposer_reason'] = explode(',',$data['proposer_reason']);
        $data['proposer_care'] = explode(',',$data['proposer_care']);

        $data['proposer_cohabit_display'] = Db::table('up_proposer_cohabit')
            ->where('cohabit_id','in',$data['proposer_cohabit'])->column('cohabit_name');
        $data['proposer_reason_display'] = Db::table('up_proposer_reason')
            ->where('reason_id','in',$data['proposer_reason'])->column('reason_name');
        $data['proposer_care_display'] = Db::table('up_proposer_care')
            ->where('care_id','in',$data['proposer_care'])->column('care_name');
        if(in_array(0,$data['proposer_cohabit'])){
            $data['proposer_cohabit_display'][] = $data['proposer_cohabit_other'];
        }
        if(in_array(0,$data['proposer_reason'])){
            $data['proposer_reason_display'][] = $data['proposer_reason_other'];
        }
        if(in_array(0,$data['proposer_care'])){
            $data['proposer_care_display'][] = $data['proposer_care_other'];
        }

        $data['proposer_cohabit_display'] = implode($data['proposer_cohabit_display'],',');
        $data['proposer_reason_display'] = implode($data['proposer_reason_display'],',');
        $data['proposer_care_display'] = implode($data['proposer_care_display'],',');

        return json_encode(['status'=>200,'msg'=>$data]);
    }

    /**
     * dog_id
     * 更新暂存的领养——获取数据
     * @return string
     */
    public function getmyAdoptDetail(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $data = Db::table('up_dog')
            ->alias('ud')
            ->join('up_u_user uuu','ud.user_id=uuu.user_id','left')
            ->join('up_dog_family udf','ud.udf_id=udf.udf_id','left')
            ->field('ud.udf_id,uuu.user_username,uuu.user_image,uuu.user_integral,uuu.user_attestation,ud.dog_release_time,ud.dog_apply_num,ud.dog_attention_num,ud.dog_adopt_status,ud.dog_adopt_experience,ud.dog_adopt_environment,ud.dog_adopt_sterilization,ud.dog_adpot_workers,ud.dog_adpot_protocol,ud.dog_adpot_certificate,ud.dog_adpot_information,ud.dog_information,ud.dog_id,udf.udf_name,udf.udf_img,ud.dog_adpot_information,ud.dog_information')
            ->where(['ud.dog_id'=>$dog_id])
            ->find();
        return json_encode(['status'=>200,'msg'=>$data]);
    }

    /**
     * NewlyAdopt 重新发布领养
     * @param $user_id 用户id
     * @param $time 时间
     * @param $value 加密值
     * @return 返回状态
     */
    public function updateMyAdopt(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        /*前台收集到的表单数据*/
        $data = [
            'dog_type'=>3,
            'user_id'=>$user_id,
            'udf_id'=>$udf_id,
            'area_sheng'=>$sheng,
            'area_shi'=>$shi,
            'area_qu'=>$qu,
            'dog_adopt_experience'=>$experience,
            'dog_adopt_environment'=>$environment,
            'dog_adopt_sterilization'=>$sterilization,
            'dog_adpot_workers'=>$workers,
            'dog_adpot_protocol'=>$protocol,
            'dog_adpot_certificate'=>$certificate,
            'dog_adpot_information'=>$adpot_information,
            'dog_information'=>$information,
            'dog_adopt_status'=>2,
            'dog_year'=>date('Y'),
            'dog_month'=>date('Y-m'),
            'dog_time' => date('Y-m-d')
        ];
        $data['dog_img'] = '';
        if($udf_id){
            $img = Db::table('up_dog_family')
                ->where('udf_id',$udf_id)
                ->value('udf_img');
            if($img){
                $data['dog_img'] = trim($img,',');
            }
        }
        if($type == 1) {
            $where = [
                'udf_id' => $udf_id,
                'dog_type' => 3,
                'dog_preserve' => 2,
            ];
            $str = Db::table('up_dog')
                ->where($where)
                ->where('dog_adopt_status', 'IN', [2, 3])
                ->find();
            if ($str) {
                $returnData['status'] = 100;
                $returnData['msg'] = "不能重复发布领养！";
                return json_encode($returnData);
            }
        }
        $time = date('Y-m-d H:i:s');
        if($type == 1){
            $data['dog_preserve'] = 2;
            $data['dog_release_time'] = $time;
            $data['dog_update_time'] = $time;
            // 启动事务
            Db::startTrans();
            try{
                Db::table('up_dog')->where('dog_id',$dog_id)
                    ->update($data);
                $res = Db::table('up_integral')
                    ->where(['integral_status'=>3,'integral_user_id'=>$user_id])
                    ->whereTime('integral_time', 'today')
                    ->count();
                if($res<2){
                    Db::table('up_integral')
                        ->insert(['integral_status'=>3,'integral_user_id'=>$user_id,'integral_time'=>date('Y-m-d H:i:s'),'integral_num'=>5,'integral_dog_id'=>$dog_id]);
                    Db::table('up_u_user')
                        ->where('user_id',$user_id)
                        ->setInc('user_integral',5);
                }
                // 提交事务
                Db::commit();
                $returnData['status'] = 200;
                $returnData['msg'] = "成功！";
                return json_encode($returnData);
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                $returnData['status'] = 0;
                $returnData['msg'] = "失败！";
                return json_encode($returnData);
            }
        }else if($type == 2){
            $data['dog_preserve'] = 1;
            $data['dog_release_time'] = $time;
            $data['dog_update_time'] = $time;
            $res = Db::table('up_dog')->where('dog_id',$dog_id)
                ->update($data);
            if($res){
                $returnData['status'] = 200;
                $returnData['msg'] = "成功！";
                return json_encode($returnData);
            }else{
                $returnData['status'] = 0;
                $returnData['msg'] = "失败！";
                return json_encode($returnData);
            }
        }

    }

    /**
     * user_id
     * 我的申请
     */
    public function myAdoptDogAsk()
    {//return json_encode(['status'=>200,'msg'=>1]);
        $uid = $_POST['user_id'];
//        $did = $_POST['dog_id'];
        $page = $_POST['page'];
         $datas = Db::table('up_dog_proposer')->alias('dp')
            ->join('up_dog d','dp.dog_id=d.dog_id','LEFT')
            ->join('up_dog_family f','d.udf_id=f.udf_id','LEFT')
            ->where('dp.proposer_user_id',$uid)
            ->order('proposer_datetime DESC')
            ->field('dp.dog_id,proposer_id,proposer_me_cancle,proposer_status,udf_name,udf_sex,udf_age,udf_dog_vaccine,udf_dog_outside,udf_dog_inside,udf_dog_sterilization,udf_dog_disability,dog_adopt_status,dog_img')
            ->limit(($page-1)*10,10)->select();
        return json_encode(['status'=>200,'msg'=>$datas]);
    }
    /**
     * proposer_id
     * 我的申请——取消申请（只有申请中的才有此操作）
     */
    public function myDeleteAdoptDogAsk()
    {
        $pid = $_POST['proposer_id'];
        $status = Db::table('up_dog_proposer')->where('proposer_id',$pid)
           ->value('proposer_status');
        if($status == 1){
            //判断是取消还是恢复
            $ot = Db::table('up_dog_proposer')->where('proposer_id',$pid)
                ->value('proposer_me_cancle');
            if($ot == 1){
                $value = 2;
            }else{
                $value = 1;
            }
            //==end===
            $res = Db::table('up_dog_proposer')->where('proposer_id',$pid)
                ->update(['proposer_me_cancle'=>$value]);
            if($res){
                return json_encode(['status'=>200,'msg'=>'操作成功']);
            }else{
                return json_encode(['status'=>0,'msg'=>'操作失败']);
            }
        }else{
            return json_encode(['status'=>0,'msg'=>'不能修改']);
        }
    }
    /**
     * proposer_id
     * 我的申请——删除申请（待用）
     */
    public function myDeleteAdoptDog()
    {
        $pid = $_POST['proposer_id'];
        $status = Db::table('up_dog_proposer')->where('proposer_id',$pid)
           ->value('proposer_status');
        if($status == 1){
            $res = Db::table('up_dog_proposer')->where('proposer_id',$pid)
                ->delete();
            if($res){
                return json_encode(['status'=>200,'msg'=>'操作成功']);
            }else{
                return json_encode(['status'=>0,'msg'=>'操作失败']);
            }
        }else{
            return json_encode(['status'=>0,'msg'=>'不能修改']);
        }
    }

}